package com.dingup.paike.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.StorageClass;
import com.suyeer.basic.util.BLogUtil;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author jun 2018/10/27
 */
public class OSSUtil {

    private static final String OSS_ACCESS_KEY_ID = "gaJAG7YTRG6GPJEC";
    private static final String OSS_ACCESS_KEY_SECRET = "cAwPb0ZxOBNFrqOF55m7nN5UCHvaK2";
    private static final String OSS_ENDPOINT = "http://oss-cn-beijing.aliyuncs.com";
    private static final ExecutorService SINGLE_THREAD_POOL = Executors.newCachedThreadPool();

    /**
     * 解冻OSS归档文件
     *
     * @param bucketName String
     * @param objectName String
     */
    public static void restoreFile(final String bucketName, final String objectName) {
        SINGLE_THREAD_POOL.execute(new Runnable() {
            @Override
            public void run() {
                restoreObject(bucketName, objectName);
            }
        });
    }

    private static void restoreObject(String bucketName, String objectName) {
        try {
            // 创建OSSClient实例。
            OSSClient ossClient = new OSSClient(OSS_ENDPOINT, OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET);
            ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, objectName);
            // 校验文件是否为归档文件。
            StorageClass storageClass = objectMetadata.getObjectStorageClass();
            if (storageClass == StorageClass.Archive) {
                // 解冻文件。
                BLogUtil.info("开始解冻文件: {}", objectName);
                ossClient.restoreObject(bucketName, objectName);
                do {
                    Thread.sleep(1000);
                    objectMetadata = ossClient.getObjectMetadata(bucketName, objectName);
                } while (!objectMetadata.isRestoreCompleted());
                BLogUtil.info("完成解冻文件: {}", objectName);
            }
            // 关闭OSSClient。
            ossClient.shutdown();
        } catch (Exception e) {
            BLogUtil.error(e);
        }
    }

    /**
     * 判断是否解冻完成
     *
     * @param bucketName String
     * @param objectName String
     * @return Boolean
     */
    public static Boolean isRestoreCompleted(String bucketName, String objectName) {
        Boolean isRestoreCompleted = false;
        try {
            // 创建OSSClient实例。
            OSSClient ossClient = new OSSClient(OSS_ENDPOINT, OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET);
            ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, objectName);
            // 校验文件是否为归档文件。
            StorageClass storageClass = objectMetadata.getObjectStorageClass();
            if (storageClass == StorageClass.Archive) {
                isRestoreCompleted = objectMetadata.isRestoreCompleted();
            }
            // 关闭OSSClient。
            ossClient.shutdown();
        } catch (Exception e) {
            BLogUtil.error(e);
        }
        return isRestoreCompleted;
    }
}
